package com.wu.framework.yearning.plus.starter.yearning.task;


import com.wu.framework.inner.lazy.database.expand.database.persistence.stream.lambda.LazyLambdaStream;
import com.wu.framework.inner.lazy.database.expand.database.persistence.stream.wrapper.LazyWrappers;
import com.wu.framework.yearning.plus.starter.yearning.LazyYearningRecord;
import com.wu.framework.yearning.plus.starter.yearning.YearningApi;
import com.wu.framework.yearning.plus.starter.yearning.api.AuditOrderListDTO;
import com.wu.framework.yearning.plus.starter.yearning.api.LoginDTO;
import com.wu.framework.yearning.plus.starter.yearning.api.command.AuditOrderListCommand;
import com.wu.framework.yearning.plus.starter.yearning.api.command.AuditOrderStateCommand;
import com.wu.framework.yearning.plus.starter.yearning.api.command.LoginCommand;
import com.wu.framework.yearning.plus.starter.yearning.config.YearningConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Objects;


@Slf4j
@Component
public class YearningTask {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    @Resource
    YearningApi yearningApi;

    @Resource
    YearningConfig yearningConfig;

    /**
     * 60秒一次
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void runYearning() {
        // 查询用户
        String username = yearningConfig.getUsername();
        String password = yearningConfig.getPassword();
        if(ObjectUtils.isEmpty(username)||ObjectUtils.isEmpty(password)){
            return;
        }
        // 登录
        LoginCommand loginCommand = new LoginCommand();
        loginCommand.setUsername(username);
        loginCommand.setPassword(password);
        LoginDTO login = yearningApi.login(loginCommand);
        log.info("登录结果:" + login);
        String token = login.getPayload().getToken();
        log.info("使用的token:{}", token);
        // 获取待审核消息
        String authorization = "Bearer " + token;
        AuditOrderListCommand auditOrderListCommand = new AuditOrderListCommand();
        auditOrderListCommand.setExpr(new AuditOrderListCommand.Expr());
        AuditOrderListDTO auditOrderListDTO = yearningApi.auditOrderList(authorization, auditOrderListCommand);
        log.info("审计信息:{}", auditOrderListDTO);

        // 判断是否需要审计
        for (AuditOrderListDTO.Data datum : auditOrderListDTO.getPayload().getData()) {

            // 判断是否已经审核过了
            String workId = datum.getWork_id();
            String sourceId = datum.getSource_id();
            String sql = datum.getSql();
            LazyYearningRecord lazyYearningRecord = lazyLambdaStream.selectOne(LazyWrappers.<LazyYearningRecord>lambdaWrapper()
                    .eq(LazyYearningRecord::getWorkId, workId));
            if (ObjectUtils.isEmpty(lazyYearningRecord) || !Objects.equals(lazyYearningRecord.getStatus(), "1")) {// 1 状态完成
                // 执行
                // 审计
                AuditOrderStateCommand auditOrderStateCommand = new AuditOrderStateCommand();
                auditOrderStateCommand.setWork_id(workId);
                auditOrderStateCommand.setSource_id(sourceId);
                String auditResult = yearningApi.auditOrderState(authorization, auditOrderStateCommand);
                log.info("审计结果:{}", auditResult);
                LazyYearningRecord story = new LazyYearningRecord();
                story.setWorkId(workId);
                story.setStatus("1");
                story.setYearningSql(sql);
                story.setAuditResult(auditResult);
                lazyLambdaStream.saveOrUpdate(story);
            } else {
                log.info("工单已经执行:{}", datum);
            }
            // 记录
        }


    }

}
